$(function(){
	$('#lableViews',window.parent.document).find("#rightMain_title").html("流量充值");

	var location = getUrlParam("sign");
//	$.ajax({			//获取登录用户信息
//			url:"user/getLoginUserJson.html",
//			type:"get",
//		 success:function(result){
//			 result=JSON.parse(result)//解析字符串
//				if(result.result!=0){//判断是否获取成功
//					 setTimeout(function(){
//		    				swal("未登录！", "点击ok登录!", "error");
//		    				$(".confirm").unbind();
//		    				$(".confirm").click(function(){
//		    					 window.location.href='login.html';
//		    				});
//	           		},0);
//				};
//				$("#userNameTitle").html(result.data.username);
//				$("#trueNameTitle").html(result.data.truename);
//		 },
//		 error:function(err){
//			 console.log(err);
//		 }
//	});


	
	
	    $.ajax({
            url:"iotsim/renewalsList",
            type:"post",
            data:{
                num: "898602B9191750169301",
                simType: "iccid",
            },
            success:function(result){
            	result=JSON.parse(result);
            	console.log(result)
            	if(result.error==0){
            		var myNum = result.result.length;
            		if (myNum==0) {
						console.log('获取套餐为空');
					}
            		var myTable = '<tbody>';
            		for(var i = 0;i<myNum;i++){
            			var period = result.result[i].package_usage/1024;
            			if(i==2){
            				myTable = myTable+'<tr><td colspan="3" style="height:10px;border:none;"></td></tr>';
                			myTable = myTable+'<td class="selMoney" name="'+period+'M"><p style="font-size:1.6rem;line-height:25px">'+result.result[i].package_name+'</p><p>套餐总量：<span class="selType">'+result.result[i].package_usage+' kb</span></p></td>'

            			}else{
                			myTable = myTable+'<td class="selMoney" name="'+period+'M"><p style="font-size:1.6rem;line-height:25px">'+result.result[i].package_name+'</p><p>套餐总量：<span class="selType">'+result.result[i].package_usage+' kb</span></p></td>'
            			}
            		}
            		myTable = myTable+'</tbody>';
            		$("#moneyTable").append(myTable);
            	    $(".selMoney").click(function(){
            			$("#payMoney").text($(this).attr("name"));
            			$("#payMoneyVal").css("display","block");
            			getQrcode();
            		});
            	}
            },
            error:function(err){
            	console.log(err)
            }
    	})
    	

	$("#backUser").click(function(){
		window.location.href = 'card.html?sign='+location;
	})
	
})

function getQrcode() {
	 $.ajax({
         url:"iotsim/getQrcode",
         type:"post",
         data:{
             num: "898602B9191750169301",
             simType: "iccid",
             packageSn: 222122
         },
         success:function(result){
        	 result=JSON.parse(result);
        	 console.log(result)
        	 if(result.error==0){
         		var myNum = result.result.length;
         		if (myNum!=0) {
         			var	qrcode_url = result.result.qrcode_url; 
         			 console.log(qrcode_url)
         			 if ( $("#qrCode").length > 0 ) { 
         				 	$("#qrCode").attr("src",qrcode_url); 
         			 }else{
         				 var image = '<img id="qrCode" src="'+qrcode_url+'"/>';
         				 $("#moneyTable").append(image);
         			 }
				}else{
					alert("二维码获取失败，请重试！")
				}
        	 }
         },
         error:function(err){
         	alert("网络异常，请重试！")
         }
	 })
}
